package modalities;

import java.util.Observer;


import javax.speech.recognition.*;

public class VoiceRecognition extends ResultAdapter {

	private ObservableThing ob;
	public String token="";
	boolean activationState = false; 

	//private Voice voice;

	public boolean isActivationState() {return activationState;}
	public void setActivationState(boolean activationState) {this.activationState = activationState;}

	public VoiceRecognition(Observer voice){
		ob = new ObservableThing();
		ob.addExternalObserver(voice);
		//this.voice = voice;
	}

	@Override
	public void resultAccepted(ResultEvent e) {
		Result r = (Result)(e.getSource());
		boolean number = true;
		ResultToken tokens[] = r.getBestTokens();

		for (int i = 0; i < tokens.length; i++){
			System.out.println(" --- " + tokens[i].getSpokenText());

		}
		for (ResultToken result : tokens)
		{
			number = true;
			System.out.println(" --- " + result.getSpokenText());
			String textVal = result.getSpokenText();
			Integer intValue = Integer.MAX_VALUE;

			try{
				intValue = 1;//Integer.parseInt(textVal);
			}
			catch(Exception ex){
				number = false;
			}

			if(number)
			{
				ob.externalSetChanged();
				ob.notifyObservers(new Message(intValue, textVal));
			}

		}
	}
}
